/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webservice.restful.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

/** Jersey REST client generated for REST resource:RSbean [product]<br>
 *  USAGE:<pre>
 *        restfulClient client = new restfulClient();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 *  </pre>
 * @author feifei
 */
public class restfulClient {
    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "https://localhost:8181/Trial-war/resources";

    public restfulClient() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig(); // SSL configuration
        config.getProperties().put(com.sun.jersey.client.urlconnection.HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new com.sun.jersey.client.urlconnection.HTTPSProperties(getHostnameVerifier(), getSSLContext()));
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("product");
    }

    public <T> T getAllCurrentItems(Class<T> responseType, Object requestEntity) throws UniformInterfaceException {
        return webResource.path("getAllCurrentItems").type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(responseType, requestEntity);
    }

    public <T> T submitItem(Class<T> responseType, Object requestEntity) throws UniformInterfaceException {
        return webResource.path("createItem").type(javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED).post(responseType, requestEntity);
    }

    public void close() {
        client.destroy();
    }

    
    
    
    private HostnameVerifier getHostnameVerifier() {
        return new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
                return true;
            }
        };
    }

    private SSLContext getSSLContext() {
        javax.net.ssl.TrustManager x509 = new javax.net.ssl.X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
                return;
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
                return;
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        SSLContext ctx = null;
        try {
            ctx = SSLContext.getInstance("SSL");
            ctx.init(null, new javax.net.ssl.TrustManager[]{x509}, null);
        } catch (java.security.GeneralSecurityException ex) {
        }
        return ctx;
    }
    
}
